conditionsPageI = 0;
conditionsPage = new Array();

function cpConditionsPage(destination, nom, valeurs, acceptSelfCat, pageNonEnregistree)
{
	this.destination = destination;
	this.nom = nom;
	this.valeurs = valeurs;
	this.acceptSelfCat = acceptSelfCat;
	this.pageNonEnregistree = pageNonEnregistree;
	
	this.comboCat = '';
	
	this.init = function()
	{
		
		var obj = this;
		
		// Code HTML initial
		$(obj.destination).append(
			'<div class="conteneurConditionsPage" id="conteneurConditionsPage'+conditionsPageI+'">&nbsp;'+
			'</div>'+
			'<a href="#" class="ajouterCondition">'+cp.msg.conditions_ajouter+'</a>'
		);
		
		// Valeurs de départ
		$.each(obj.valeurs, function(i, valeur) {
			html = obj.nouvelleCondition(valeur);
			$(obj.destination).find('div.conteneurConditionsPage').append(html);
		});
		obj.parseSeparateur();
		$(obj.destination).find('p.condition').each(function() {	
			obj.initCondition(this);
		});

		// Page non enregistrée
		if(obj.pageNonEnregistree == 1) {
			$(obj.destination).find('select').live('change', function() { cp.setPageModifiee(); });
			$(obj.destination).find('input').live('change', function() { cp.setPageModifiee(); });
		}
		
		// Suppression de condition
		$('#conteneurConditionsPage'+conditionsPageI+'>p>a.supprimerCondition').live('click', function() {
			$(this).parent().supprimerAvecEffet(function() {
				obj.parseSeparateur();
			});
			if(obj.pageNonEnregistree == 1) {
				cp.setPageModifiee();
			}
			return false;
		});
		
		// Ajout de condition
		valeursDefaut = new Array('and', 'page_id', '=', '');
		$(obj.destination).find('a.ajouterCondition').click(function() {
			$(obj.destination).find('div.conteneurConditionsPage').append(obj.nouvelleCondition(valeursDefaut));
			obj.parseSeparateur();
			obj.initCondition(obj.destination.find('div.conteneurConditionsPage').find('p.condition:last'));
			if(obj.pageNonEnregistree == 1) {
				cp.setPageModifiee();
			}
			return false;
		});	
		
	};
	
	this.nouvelleCondition = function(valeur)
	{
		
		var obj = this;
		
		return '<p class="condition">'+
			'<span class="conditionSeparateur">'+obj.separateur(valeur[0])+'&nbsp;</span>'+
			'<span class="conditionChamp">'+obj.champ(valeur[1])+'</span>'+
			'<span class="conditionOperateur">'+obj.operateur(valeur[1], valeur[2])+'</span>'+
			'<span class="conditionValeur">'+obj.valeur(valeur[1], valeur[3])+'</span>'+
			'<a href="#" class="supprimerCondition" title="'+cp.msg.conditions_supprimer+'"><img src="images/16/supprimer.png" alt="'+cp.msg.conditions_supprimer+'" /></a>'+
		'</p>';
	
	};
	
	this.initCondition = function(condition)
	{
		
		var obj = this;
		
		var champInitial = $(condition).find('span.conditionChamp>select').val(); 
		if(champInitial == 'page_date' || champInitial == 'page_date_modification') {
			obj.initCalendrier(condition);
			$(condition).find('span.conditionOperateur>select').change(function() {
				obj.initCalendrier($(this).parent().parent());
			});
		} else if(champInitial == 'cat_id') {
			obj.initComboCat(condition);
		} else if (champInitial == 'page_id') {
			obj.initSelecteurPage(condition);
		} else if( champInitial == 'utilisateur_id') {
			obj.initSelecteurUtilisateur(condition);
		}
		
		$(condition).find('span.conditionChamp>select').change(function() {
			var champ = $(this).val();
			$(this).parent().parent().find('span.conditionOperateur').html(obj.operateur(champ, '='));
			$(this).parent().parent().find('span.conditionValeur').html(obj.valeur(champ, ''));

			if(champ == 'page_date' || champ == 'page_date_modification') {
				obj.initCalendrier($(this).parent().parent());
				$(this).parent().parent().find('span.conditionOperateur>select').change(function() {
					obj.initCalendrier($(this).parent().parent());
				});		
			} else if(champ == 'cat_id') {
				obj.initComboCat(condition);
			} else if(champ == 'page_id') {
				obj.initSelecteurPage(condition);
			} else if( champ == 'utilisateur_id') {
				obj.initSelecteurUtilisateur(condition);
			}
		});
		
	};
	
	this.initCalendrier = function(condition) {
		
		$(condition).find('span.conditionValeur>input').datepicker('destroy');
		
		var operateur = $(condition).find('span.conditionOperateur>select').val();

		if(operateur == '=' || operateur == '!=' || operateur == 'after' || operateur == 'before') {
			$(condition).find('span.conditionValeur>input').datepicker({dateFormat: 'dd-mm-yy'});
		}
		
	};

	
	this.parseSeparateur = function()
	{
		
		var obj = this;
		
		var first = true;
		$(obj.destination).find('span.conditionSeparateur select').each(function() {
			if(first) {
				$(this).css('display', 'none');
			} else {
				$(this).css('display', 'block');
			}
			first = false;
		});
	};
	
	this.initSelecteurPage = function(condition)
	{
		var obj = this;
		
		$(condition).find('a.selecteur_page_choisir').each(function() {
			cp.initSelecteurPage(	$(this),
									$(this).parent().find('input'), 
									$(this).parent().find('span.selecteur_page_titre')
			);
		});
	};
	
	this.initSelecteurUtilisateur = function(condition)
	{
		var obj = this;
		
		$(condition).find('a.selecteur_utilisateur_choisir').each(function() {
			cp.initSelecteurUtilisateur($(this),
										$(this).parent().find('input'), 
										$(this).parent().find('span.selecteur_utilisateur_pseudo')
			);
		});
	};
	
	this.initComboCat = function(condition)
	{
		var obj = this;
		
		// Chargement du combo des catégories
		if(obj.comboCat == '') {
			cp.ajaxService({
				action:			'cp-pages-getComboCategories',
				nom:			obj.nom+'[4][]',
				self:			obj.acceptSelfCat
			}, function(reponse) {
				obj.comboCat = $('html', reponse).text();
				obj.initComboCat_exec(condition);
			});
		} else {
			obj.initComboCat_exec(condition);
		}
	};
	
	this.initComboCat_exec = function(condition) 
	{
		var obj = this;
		var val = $(condition).find('span.conditionValeur>input').val();
		
		$(condition).find('span.conditionValeur').html(obj.comboCat).find('select').val(val);
	};

	this.separateur = function(defaut)
	{
		var obj = this;
		
		andSelected = '';
		orSelected = '';
		
		if(defaut == 'and')		{ andSelected = 'selected="selected"'; }
		else if(defaut == 'or') { orSelected = 'selected="selected"'; }
		
		return '<select name="'+obj.nom+'[1][]">'+
			'<option value="and" '+andSelected+'>'+cp.msg.conditions_et+'</option>'+
			'<option value="or" '+orSelected+'>'+cp.msg.conditions_ou+'</option>'+
		'</select>';
	};
	
	this.champ = function(defaut)
	{
		
		var obj = this;
		
		var page_id_selected 				= '';
		var page_titre_selected 			= '';
		var tag_selected					= '';
		var page_url_selected 				= '';
		var cat_id_selected 				= '';
		var page_date_selected				= '';
		var page_date_modification_selected = '';
		var utilisateur_id_selected			= '';
		var page_langue_selected			= '';
		
		if(defaut=='page_id') 						{ page_id_selected 					= 'selected="selected"'; }
		else if(defaut=='page_titre') 				{ page_titre_selected 				= 'selected="selected"'; }
		else if(defaut=='tag') 						{ tag_selected 						= 'selected="selected"'; }
		else if(defaut=='page_url') 				{ page_url_selected 				= 'selected="selected"'; }
		else if(defaut=='cat_id') 					{ cat_id_selected 					= 'selected="selected"'; }
		else if(defaut=='page_date') 				{ page_date_selected 				= 'selected="selected"'; }
		else if(defaut=='page_date_modification') 	{ page_date_modification_selected 	= 'selected="selected"'; }
		else if(defaut=='utilisateur_id') 			{ utilisateur_id_selected 			= 'selected="selected"'; }
		else if(defaut=='page_langue') 				{ page_langue_selected 				= 'selected="selected"'; }
		
		return 	'<select name="'+obj.nom+'[2][]">'+
			'<option value="page_id" '+page_id_selected+'>'+cp.msg.conditions_page+'</option>'+
			'<option value="page_titre" '+page_titre_selected+'>'+cp.msg.conditions_titre+'</option>'+
			'<option value="tag" '+tag_selected+'>'+cp.msg.conditions_tag+'</option>'+
			'<option value="page_url" '+page_url_selected+'>'+cp.msg.conditions_url+'</option>'+
			'<option value="cat_id" '+cat_id_selected+'>'+cp.msg.conditions_dossier+'</option>'+
			'<option value="page_date" '+page_date_selected+'>'+cp.msg.conditions_date_creation+'</option>'+
			'<option value="page_date_modification" '+page_date_modification_selected+'>'+cp.msg.conditions_date_modification+'</option>'+
			'<option value="utilisateur_id" '+utilisateur_id_selected+'>'+cp.msg.conditions_auteur+'</option>'+
			'<option value="page_langue" '+page_langue_selected+'>'+cp.msg.conditions_langue+'</option>'+
		'</select>';
		
	};
	
	this.operateur = function(champ, defaut)
	{
		
		var obj = this;
		
		egal_selected 				= '';
		pasEgal_selected			= '';
		like_selected				= '';
		notLike_selected			= '';
		apres_selected				= '';
		avant_selected 				= '';
		ilYaplus_jours_selected		= '';
		ilYamoins_jours_selected	= '';
		ilYaplus_heures_selected	= '';
		ilYamoins_heures_selected	= '';
		
		if(defaut=='=')				{ egal_selected 	= 'selected="selected"'; }
		else if(defaut=='!=')		{ pasEgal_selected 	= 'selected="selected"'; }
		else if(defaut=='like')		{ like_selected 	= 'selected="selected"'; }
		else if(defaut=='notLike')	{ notLike_selected 	= 'selected="selected"'; }
		else if(defaut=='after')	{ apres_selected 	= 'selected="selected"'; }
		else if(defaut=='before')	{ avant_selected 	= 'selected="selected"'; }
		else if(defaut=='il_y_a_plus_jours')		{ ilYaplus_jours_selected 	= 'selected="selected"'; }
		else if(defaut=='il_y_a_moins_jours')	{ ilYamoins_jours_selected 	= 'selected="selected"'; }
		else if(defaut=='il_y_a_plus_heures')	{ ilYaplus_heures_selected 	= 'selected="selected"'; }
		else if(defaut=='il_y_a_moins_heures')	{ ilYamoins_heures_selected = 'selected="selected"'; }
		
		var html = '<select name="'+obj.nom+'[3][]">'+
			'<option value="=" '+egal_selected+'>'+cp.msg.conditions_est+'</option>'+
			'<option value="!=" '+pasEgal_selected+'>'+cp.msg.conditions_est_pas+'</option>';
		
		if(	champ	== 'page_titre'	||
			champ	== 'page_url'
		) {
			html = html+
			'<option value="like" '+like_selected+'>LIKE</option>'+
			'<option value="notLike" '+notLike_selected+'>NOT LIKE</option>';
		}
		
		if(	champ	== 'page_date'	||
			champ	== 'page_date_modification'
		) {
			html = html+
			'<option value="after" '+apres_selected+'>'+cp.msg.conditions_est_apres+'</option>'+
			'<option value="before" '+avant_selected+'>'+cp.msg.conditions_est_avant+'</option>'+
			'<option value="il_y_a_plus_jours" '+ilYaplus_jours_selected+'>'+cp.msg.conditions_plus_jours+'</option>'+
			'<option value="il_y_a_moins_jours" '+ilYamoins_jours_selected+'>'+cp.msg.conditions_moins_jours+'</option>'+
			'<option value="il_y_a_plus_heures" '+ilYaplus_heures_selected+'>'+cp.msg.conditions_plus_heures+'</option>'+
			'<option value="il_y_a_moins_heures" '+ilYamoins_heures_selected+'>'+cp.msg.conditions_moins_heures+'</option>';
		}
		
		html = html+
		'</select>';
		
		return html;
		
	};
	
	this.valeur = function(champ, defaut)
	{
		var obj = this;
		
		if(	champ == 'page_id') {
			return '<span class="selecteur_page_titre">'+cp.msg.selecteur_pas_selection+'</span>'+
			'<input class="selecteur_page_id" type="hidden" name="'+obj.nom+'[4][]" value="'+defaut+'" />'+
			'<a href="#" class="selecteur_page_choisir">'+cp.msg.selecteur_choisir+'</a>';
		} else if(champ == 'utilisateur_id') {
			return '<span class="selecteur_utilisateur_pseudo">'+cp.msg.selecteur_pas_selection+'</span>'+
			'<input class="selecteur_utilisateur_id" type="hidden" name="'+obj.nom+'[4][]" value="'+defaut+'" />'+
			'<a href="#" class="selecteur_utilisateur_choisir">'+cp.msg.selecteur_choisir+'</a>';
		} else if(champ == 'page_langue') {
			return '<select name="'+obj.nom+'[4][]">'+
				'<option value="en">English</option>'+
				'<option value="fr">Français</option>'+
			'</select>';
		}
		
		return '<input type="text" size="40" name="'+obj.nom+'[4][]" value="'+defaut+'" />';
	};
	
}

$.fn.cpConditionsPage = function(nom, valeurs, acceptSelfCat, pageNonEnregistree) {

	return this.each(function() {
		dest = $(this);
		conditionsPageI++;
		conditionsPage[conditionsPageI] = new cpConditionsPage(dest, nom, valeurs, acceptSelfCat, pageNonEnregistree);
		conditionsPage[conditionsPageI].init();
	});

};